#!/data/Software/mydan/perl/bin/perl -I/data/Software/mydan/AGENT/lib -I/data/Software/mydan/AGENT/private/lib
use strict;
use warnings;
use FindBin qw( $RealBin );
use POSIX;
use Util;
use Logs;

$| ++;

=head1 SYNOPSIS

    db => $mysql,

=cut

return sub
{
    my %param = @_;

    my ( $myname, $db, $logs ) = ( Util::myname(), $param{db}, Logs->new( 'bury' ) );

    while(1)
    {
        warn "do ...\n";

        my $x = eval{ $db->query( "select id,pid,uuid,starttime,starttimems from install 
            where slave='$myname' and pid is not null and status<>'success' and status<>'fail'" );};
        $logs->die( "mysql query fail: $@" ) if $@;
        $logs->die( "get list fail from mysql" ) unless defined $x && ref $x eq 'ARRAY';

        for my $r ( @$x )
        {
            my ( $id, $pid, $uuid, $starttime, $starttimems ) = @$r;
            next unless $pid =~ /^\d+$/;
            next unless $starttimems =~ /^[\.\d]+$/;
            next unless $uuid =~ /^[a-zA-Z0-9]+$/;

            next if kill( 0, $pid );

            my $mtime = ( stat  "$RealBin/../logs/install/$uuid" )[9];

            my ( $runtime, $finishtime, $finishtimems );

            if( $mtime )
            {
                $mtime ++ if $mtime < $starttimems;
                $mtime = $starttimems if $mtime < $starttimems;
                $runtime = sprintf "%0.3f", $mtime - $starttimems;
                $finishtime = POSIX::strftime( "%Y-%m-%d %H:%M:%S", localtime( int $mtime ) );
                $finishtimems = $mtime;
            }
            else
            {
                ( $runtime, $finishtime, $finishtimems ) = ( 0, $starttime, $starttimems );
            }

            eval{ $db->execute( "update install set status='fail',runtime='$runtime',
                finishtime='$finishtime',finishtimems='$finishtimems' where id=$id" );};
            $logs->err( "update install status fail: id=$id :$@" ) if $@;
        }
        sleep 3;
    }
}
